import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import maya.OpenMayaAnim as OpenMayaAnim
import math
import masdevallia.utility.Dag as Dag

obj_name = 'pSphere1'
obj = Dag.getMObject( obj_name )
dag = Dag.getDagPath( obj )

anim_curve_fn = OpenMayaAnim.MFnAnimCurve()

x_pos_plug = Dag.getPlugByName( obj, 'translateX' )
x_pos_array = OpenMaya.MObjectArray()
if not OpenMayaAnim.MAnimUtil.findAnimation( x_pos_plug, x_pos_array ):
    x_pos_anim_node = anim_curve_fn.create( x_pos_plug )
else:
    x_pos_anim_node = x_pos_array[0]

z_rot_plug = Dag.getPlugByName( obj, 'rotateZ' )
z_rot_array = OpenMaya.MObjectArray()
if not OpenMayaAnim.MAnimUtil.findAnimation( z_rot_plug, z_rot_array ):
    z_rot_anim_node = anim_curve_fn.create( z_rot_plug )
else:
    z_rot_anim_node = z_rot_array[0]


anim_curve_fn.setObject( x_pos_anim_node )

for keyframe_idx in xrange( anim_curve_fn.numKeys() ):
    if keyframe_idx == 1:
        #angle = OpenMaya.MAngle()
        #util = OpenMaya.MScriptUtil()
        #util.createFromDouble(0.0)
        #ptr = util.asDoublePtr()
        
        util_x = OpenMaya.MScriptUtil()
        ptr_x = util_x.asFloatPtr()
        
        util_y = OpenMaya.MScriptUtil()
        ptr_y = util_y.asFloatPtr()
        
        anim_curve_fn.getTangent( keyframe_idx, ptr_x, ptr_y, False )
        
        x = util_x.getFloat( ptr_x )
        y = util_y.getFloat( ptr_y )
        
        #seconds for time, centimeters for linear, radians for angles
        print 'TRANSLATE X'
        print '    {0}'.format( anim_curve_fn.animCurveType() )
        print '    {0}, {1}'.format( x, y )
        print '    X: {0}'.format( OpenMaya.MTime( x, OpenMaya.MTime.kSeconds ).asUnits( OpenMaya.MTime.uiUnit() ) )
        print '    Y: {0}'.format( OpenMaya.MDistance( y, OpenMaya.MDistance.kCentimeters ).asCentimeters() )



anim_curve_fn.setObject( z_rot_anim_node )

for keyframe_idx in xrange( anim_curve_fn.numKeys() ):
    if keyframe_idx == 1:
        #angle = OpenMaya.MAngle()
        #util = OpenMaya.MScriptUtil()
        #util.createFromDouble(0.0)
        #ptr = util.asDoublePtr()
        
        util_x = OpenMaya.MScriptUtil()
        ptr_x = util_x.asFloatPtr()
        
        util_y = OpenMaya.MScriptUtil()
        ptr_y = util_y.asFloatPtr()
               
        anim_curve_fn.getTangent( keyframe_idx, ptr_x, ptr_y, False )
                
        x = util_x.getFloat( ptr_x )
        y = util_y.getFloat( ptr_y )
        
        print 'ROTATE Z'
        print '    {0}'.format( anim_curve_fn.animCurveType() )
        print '    {0}, {1}'.format( x, y )
        print '    X: {0}'.format( OpenMaya.MTime( x, OpenMaya.MTime.kSeconds ).asUnits( OpenMaya.MTime.uiUnit() ) )
        print '    Y: {0}'.format( OpenMaya.MAngle( y, OpenMaya.MAngle.kRadians ).asDegrees() )
        
        
        angle = OpenMaya.MAngle()
        weight_util = OpenMaya.MScriptUtil()
        weight = weight_util.asDoublePtr()
        anim_curve_fn.getTangent( keyframe_idx, angle, weight, False )        
        print '    angle: {0}'.format( angle.asDegrees() )
        print '    weight: {0}'.format( weight_util.getDouble(weight) )
        
# 11 frames, 40 value
x = 40 * 0.25
y = 11 * 0.5
#print x, y
angle = OpenMaya.MAngle( math.atan2(y,x) )
#print math.atan2(y,x) * (180/math.pi)
print 'angle: {0}'.format( angle.asDegrees() )
weight = x / ( 3 * math.cos(angle.asDegrees()) )
#weight = math.sqrt( (y ** 2)+(x ** 2) )
print 'weight: {0}'.format( weight )
#anim_curve_fn.setTangent( 1, angle, weight, False )

#Tangent angle equals atan(dQ/dt)
#0.25, 0, 0.75, 1
cx = 0.25
cy = 1.0
nx = 0.75
ny = 1.0

print OpenMaya.MAngle( 90.0, OpenMaya.MAngle.kDegrees ).asRadians()


cur_time = anim_curve_fn.time( 1 )
next_time = anim_curve_fn.time( 2 )
diff_time = abs( (next_time.value() - cur_time.value()) + 1 )
print diff_time
dt = (diff_time * cx)# * 10
print 'dt: {0}'.format( dt )

#get the two key values, find their difference.
#take the percentage of the difference.
#convert that to radians to get the tangent angle
cur_value = OpenMaya.MAngle( anim_curve_fn.value( 1 ), OpenMaya.MAngle.kRadians )
next_value = OpenMaya.MAngle( anim_curve_fn.value( 2 ), OpenMaya.MAngle.kRadians )
diff_value = abs( next_value.asDegrees() - cur_value.asDegrees() )
perc = diff_value * cy
print perc
dQ = OpenMaya.MAngle( perc, OpenMaya.MAngle.kDegrees ).asRadians()
print 'dQ: {0}'.format( dQ )

#angle = OpenMaya.MAngle( math.atan2( dQ, dt ), OpenMaya.MAngle.kRadians )
angle = OpenMaya.MAngle( dQ, OpenMaya.MAngle.kRadians )
print 'angle: {0}'.format( angle.asRadians() )
print 'angle: {0}'.format( angle.asDegrees() )

#tangent weight is length of the handle, which equals sqrt(dQ*dQ + dt*dt).
weight = math.sqrt( dQ*dQ + dt*dt )
#weight = cx / ( 3 * math.cos( angle.asRadians() ) )

print 'weight: {0}'.format( weight )

anim_curve_fn.setTangent( 1, angle, weight, False )
#anim_curve_fn.setTangent( 1, x, y, False, None, True )


n_dt = time_span * ( 1 - nx )
print 'n_dt: {0}'.format( n_dt )

n_dQ = next_value.asRadians() + ( value_span * ny )
print 'n_dQ: {0}'.format( n_dQ )

angle = OpenMaya.MAngle( math.atan2( n_dQ, n_dt ), OpenMaya.MAngle.kRadians )
print 'angle: {0}'.format( angle.asDegrees() )

weight = math.sqrt( n_dQ*n_dQ + n_dt*n_dt )
print 'weight: {0}'.format( weight )

#anim_curve_fn.setTangent( 2, angle, weight, True )



















#x = 3 * weight * math.cos( angle )
#y = 3 * weight * math.sin( angle )
#print x, y
#anim_curve_fn.setTangent( 1, x, y, False )
#print cmds.keyTangent( 'pSphere1', index=(1,), query=True, attribute='rotateZ', ox=True, oy=True )








#angle = atan(y/x)
xx = -0.726817
yy = 0.236293
tangle = math.atan2( xx, yy )
#print tangle
#weight = x/(3*cos(angle))
weight = 0.236293 / ( 3 * math.cos( tangle ) )


#x = 3 * weight * cos(angle)
x = 3 * weight * math.cos( tangle )

#y = 3 * weight * sin(angle)
y = 3 * weight * math.sin( tangle )

#print x, y
#anim_curve_fn.setTangent( 1, x, y, False )












